package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @program: gmall-parent
 * @author: Mr.Zhuo
 * @create: 2022-04-03 14:40
 **/
@Service
public class BaseTrademarkServiceImpl extends ServiceImpl<BaseTrademarkMapper, BaseTrademark> implements BaseTrademarkService {

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;


    //品牌管理保存
    @Override
    public void mySave(BaseTrademark baseTrademark) {
        // 首先判断是否存在相同品牌
        QueryWrapper<BaseTrademark> wrapper = new QueryWrapper<>();
        wrapper.eq("tm_name", baseTrademark.getTmName());
        BaseTrademark one = baseTrademarkMapper.selectOne(wrapper);

        if (StringUtils.isEmpty(one)) {
            if (StringUtils.isEmpty(baseTrademark.getTmName())){
                return;
            }
            // 不存在进行插入
            baseTrademarkMapper.insert(baseTrademark);
        } else {
            // 若存在则  更更新
            one.setLogoUrl(baseTrademark.getLogoUrl());
            baseTrademarkMapper.updateById(one);
        }

    }

    //品牌管理分页列表
    @Override
    public IPage<BaseTrademark> baseTrademark(Page<BaseTrademark> trademarkPage) {
        // select * from base_trademark
        QueryWrapper<BaseTrademark> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");

        // 返回数据
        return baseTrademarkMapper.selectPage(trademarkPage, null);
    }
}
